package com.hu.biz.encrypt;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 00351634
 * @version 1.0
 * @date 2024/10/15 8:05
 * @description: AES加解密处理器
 */
@NoArgsConstructor
@Slf4j
public class AesEncryptProcessor implements EncryptProcessor {
    private AES aes;

    @Override
    public String getAlgorithm() {
        return "aes";
    }

    @Override
    public void initAlgorithm(String secret) {
        this.aes = SecureUtil.aes(Base64.decode(secret));
    }

    @Override
    public String encode(final Encrypt encrypt) {
        log.info("加密前：{}", encrypt.getValue());
        final String encryptValue = this.aes.encryptHex(encrypt.getValue());
        log.info("加密后：{}", encryptValue);
        return encryptValue;
    }

    @Override
    public Encrypt decode(final String value) {
        log.info("解密前：{}", value);
        final String decryptValue = this.aes.decryptStr(value);
        log.info("解密后：{}", decryptValue);
        return new Encrypt(decryptValue);
    }

}
